package com.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pojo.MyParams;
import com.service.ParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/param")
public class ParamController {

    @Autowired
    private ParamService paramService;

    @GetMapping("/list")
    public String list(@RequestParam(defaultValue = "1") int pageNum,
                       @RequestParam(defaultValue = "10") int pageSize,
                       Model model) {

        PageHelper.startPage(pageNum, pageSize);
        List<MyParams> paramsList = paramService.findAll();
        PageInfo<MyParams> pageInfo = new PageInfo<>(paramsList);
        model.addAttribute("pageInfo", pageInfo);
        return "param/paramList";
    }

    @GetMapping("/add")
    public String addPage() {
        return "param/paramAdd";
    }

    @PostMapping("/insert")
    public String insert(MyParams myParams) {
        paramService.insert(myParams);
        return "redirect:/param/list";
    }

    @GetMapping("/edit/{id}")
    public String editPage(@PathVariable Integer id, Model model) {
        MyParams myParams = paramService.findById(id);
        model.addAttribute("myParams", myParams);
        return "param/paramEdit";
    }

    @PostMapping("/update")
    public String update(MyParams myParams) {
        paramService.update(myParams);
        return "redirect:/param/list";
    }

    @GetMapping("/delete/{id}")
    public String delete(@PathVariable Integer id) {
        paramService.deleteById(id);
        return "redirect:/param/list";
    }
}
